package com.fmsz.xmly.asynctasks;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.fmsz.xmly.callbacks.InterfaceClass;
import com.fmsz.xmly.entitys.FindTabEntity;
import com.fmsz.xmly.utils.MyHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */

public class FindTabDown extends AsyncTask<String,Void,String> {
    InterfaceClass.FindTabCallback callback;

    public FindTabDown(InterfaceClass.FindTabCallback callback) {
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params) {
        String json= MyHttpUtils.getStringFromUrl(params[0]);
        return json;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (!TextUtils.isEmpty(s)) {
            List<FindTabEntity> list=getList(s);
            callback.findTabCallback(list);
        }
    }

    private List<FindTabEntity> getList(String json) {
        List<FindTabEntity> list=new ArrayList<>();
        try {
            JSONObject obj1=new JSONObject(json);
            JSONObject tabs = obj1.getJSONObject("tabs");
            JSONArray list1 = tabs.getJSONArray("list");
            for (int i = 0; i < list1.length(); i++) {
                JSONObject obj2 = list1.getJSONObject(i);
                String title = obj2.getString("title");
                String contentType = obj2.getString("contentType");
                list.add(new FindTabEntity(title,contentType));
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
